package com.wythe.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.model.GlideLoader;
import com.yancy.imageselector.ImageConfig;
import com.yancy.imageselector.ImageSelector;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 2016/12/29.
 * 发布图文 图片列表适配器
 */
public class PublishPicAndMessAdapter extends BaseAdapter implements View.OnClickListener {

    private final int MAX_COUNT = 3;
    private ArrayList<String> mImages;
    private LayoutInflater mLayoutInflater;

    private Context mContext;
    private OnImagesChangedCallback mCallBack;

    public PublishPicAndMessAdapter(Context context, OnImagesChangedCallback callback) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mCallBack = callback;
    }

    public void setImages(ArrayList<String> list) {
        if (mImages == null) {
            mImages = new ArrayList<>();
        }
        mImages.clear();
        if (list != null && list.size() > 0) {
            mImages.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int imageCount = mImages != null ? mImages.size() : 0;
        return (imageCount >= 0 && imageCount < MAX_COUNT) ? (imageCount + 1) : imageCount;
    }

    @Override
    public String getItem(int position) {
        if (position >= 0 && mImages != null && position < mImages.size()) {
            return mImages.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_trailermessage_image, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String url = getItem(position);
        if (url == null) {
            holder.btnDelete.setVisibility(View.GONE);
            holder.btnDelete.setOnClickListener(null);
            Glide.with(mContext)
                    .load(R.drawable.notice__add_to)
                    .dontAnimate()
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.ivPreview);
        } else {
            holder.btnDelete.setVisibility(View.VISIBLE);
            holder.btnDelete.setTag(url);
            holder.btnDelete.setOnClickListener(this);
            Glide.with(mContext)
                    .load(url)
                    .dontAnimate()
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.ivPreview);
        }
        return convertView;
    }

    public void showImageSelector(Activity context, int requestCode) {
        ImageConfig.Builder build = new ImageConfig.Builder(new GlideLoader())
                // 如果在 4.4 以上，则修改状态栏颜色 （默认黑色）
//                .steepToolBarColor(mContext.getResources().getColor(R.color.white))
                // 标题的背景颜色 （默认黑色）
                .titleBgColor(context.getResources().getColor(R.color.white))
                // 提交按钮字体的颜色  （默认白色）
                .titleSubmitTextColor(context.getResources().getColor(R.color.text_black_main))
                // 标题颜色 （默认白色）
                .titleTextColor(context.getResources().getColor(R.color.text_black_main))
                .requestCode(requestCode)
                .filePath("/" + context.getPackageName() + "/image/");
        build.mutiSelect();
        build.showCamera();
        if (mImages != null && mImages.size() > 0) {
            ArrayList<String> list = (ArrayList<String>) mImages.clone();
            build.pathList(list);
        }
        ImageSelector.open(context, build.build());
    }

    public List<String> getDataList() {
        return mImages;
    }

    @Override
    public void onClick(View v) {
        String url = (String) v.getTag();
        mImages.remove(url);
        mCallBack.onImagesChanged();
        notifyDataSetChanged();
    }
    class ViewHolder {
        ImageView ivPreview,btnDelete;

        ViewHolder(View itemView) {
            ivPreview = (ImageView) itemView.findViewById(R.id.iv_preview);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            itemView.setTag(this);
        }

    }
    /**
     * 图片变化回调
     */
    public interface OnImagesChangedCallback {
        /**
         * 图片更改
         */
        void onImagesChanged();
    }
}
